<?php 
class shop_admin extends MX_Controller{
	public function index(){
		$limit=25;
		$paged=$this->input->post('paged')?$this->input->post('paged'):1;
		$filter=array('category'=>0,'date'=>'');
		$paging=array('page_current'=>0,'number_affiche'=>$limit);
		$adminMenu= $this->load->module('adminMenu/index');
		$category= $this->load->module('category/admin');
		
		$sort=array('company_name'=>'desc','shop_counter'=>'desc');
		if(isset($_POST['sort']))
			foreach ($sort as $key=>$val)
				$sort[$key]=$this->input->post($key);
		
		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		//prendre tous les id des produits
		$listAllShop=$this->model()->get_list_shop($filter,$sort,$paging);
		
		$option_filter_category=$category->wp_dropdown_cats($filter['category']);
		$option_filter_date=$this->model()->get_month_year_create();
		$category->listTableCategory();
		$data=array(
				'update_footer'=>'StarView 2013',
				'admin_title'=>'Control Admin Boutique',
				'sort'=>$sort,
				'filter'=>$filter,
				'option_filter_category'=>$option_filter_category,
				'option_filter_date'=>$option_filter_date,
				'admin_menu'=>$adminMenu->generateAdminMenu(),
				'listAllProducts'=>$listAllShop,
				'tableCategoryBody'=>$category->category,
				'limit'=>$limit,
				'paged'=>$paged,
				'totalActive'=>$this->model()->get_total_shop(true),
				'total'=>$this->model()->get_total_shop()
		);
		
		
		$this->view(
				array('admin-header',
						'shop',
						'admin-footer'),
				$data); // Display the page
	}
	public function new_shop(){
		$adminMenu= $this->load->module('adminMenu/index');
		$category= $this->load->module('category/admin');
	
		$localisation= $this->load->module('localisation/index');
		$location=$localisation->getlocation();
		$regionList=$location->OptionRegion();
		$departementList=$location->OptionDepartement();
		$codePostal=$location->Postalcode;
		$villeList=$location->OptionVille($codePostal);
	

		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		$data=array(
				'update_footer'=>'StarView 2013',
				'admin_title'=>'Control Admin',
				'admin_menu'=>$adminMenu->generateAdminMenu(),
				'regionList'=>$regionList,
				'departementList'=>$departementList,
				'villeList'=>$villeList,
				'codePostal'=>$codePostal,
				'CategoryList'=>$category->dropdown_option_cats()
		);
	
		$this->view(
				array('admin-header',
						'shop-new',
						'admin-footer'),
					$data); // Display the page
		}
		public function create(){

			$this->load->helper('url');
			$shop=new stdclass;
			$shop->company_name= $this->input->post('company_name');
			$shop->tilte= $this->input->post('tilte');
			$shop->subtilte= $this->input->post('subtilte');
			$shop->shop_link= $this->input->post('shop_link');
			$shop->description= $this->input->post('description');
			$shop->shop_active= $this->input->post('shop_active');
			$shop->date_create= strtotime('now');
			$shop->date_validate= strtotime($this->input->post('aa').'-'.$this->input->post('mm').'-'.$this->input->post('jj').'-'.$this->input->post('aa').' '.$this->input->post('hh').':'.$this->input->post('mn').' 00');
			$shop->shop_tag= $this->input->post('shop_tag');
			$shop->shop_keywords= $this->input->post('shop_keywords');
			$shop->siren= $this->input->post('siren');
			$shop->adresse= $this->input->post('adresse');
			$shop->region= $this->input->post('region');
			$shop->departement= $this->input->post('departement');
			$shop->code_postal= $this->input->post('code_postal');
			$shop->email= $this->input->post('email');
			$shop->password= $this->input->post('password');
			$shop->frist_name= ucwords(strtolower($this->input->post('frist_name')));
			$shop->last_name= strtoupper($this->input->post('last_name'));
			$shop->site= $this->input->post('site');
			$shop->open= $this->input->post('open');
			$shop->category_id= $this->input->post('category_id');
			$shop->telephone= $this->input->post('telephone');
			$shop->logo=  url_title($shop->company_name.'-'.$shop->siren);
			$shop->youtube= $this->input->post('youtube');
			
			$setting= $this->load->module('setting/medias');
			$medias=$setting->get_Medias();
			$config['upload_path'] = './images/logos/';
			$config['file_name'] =  $shop->logo;
			$config['overwrite'] = TRUE;
			$config['allowed_types'] = $medias['allowed_types'];
			$config['max_size']	= $medias['max_size'];
			
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('logo')){
				$error = array('error' => $this->upload->display_errors());
			}else{
				$data = array('logo' => $this->upload->data());
			
				//[ MAIN IMAGE ]
				$config2['image_library'] = 'gd2';
				$config2['source_image'] = $this->upload->upload_path.$this->upload->file_name;
				$config2['new_image'] = './images/logos/'.$this->upload->file_name;
				$config2['maintain_ratio'] = TRUE;
				$config2['overwrite'] = TRUE;
				$config2['width'] = $medias['large_size_w'];
				$config2['height'] = $medias['large_size_h'];
				$this->load->library('image_lib',$config2);
				
				if ( !$this->image_lib->resize()){
					$error = array('error' => $this->image_lib->display_errors('', ''));
				}
				////[ THUMB IMAGE ]
				$config2['new_image'] = './images/logos/thumbnails/'.$this->upload->file_name;				
				$config2['width'] = $medias['thumbnail_size_w'];
				$config2['height'] = $medias['thumbnail_size_h'];
				$this->load->library('image_lib',$config2);
				$this->image_lib->initialize($config2);
				if ( !$this->image_lib->resize()){
					$error = array('error' => $this->image_lib->display_errors('', ''));
				}
				$shop->logo=$this->upload->file_name;
			}
			
			$this->model()->create($shop);
			global $CFG;
			$url=$CFG->config['base_url'].'shop/admin';
			redirect($url);
		}
		public function edit_shop(){
		
			$this->load->helper('url');
			$shop=new stdclass;
			$shop->id= $this->input->post('id');
			$shop->company_name= $this->input->post('company_name');
			$shop->tilte= $this->input->post('tilte');
			$shop->subtilte= $this->input->post('subtilte');
			$shop->shop_link= $this->input->post('shop_link');
			$shop->description= $this->input->post('description');
			$shop->shop_active= $this->input->post('shop_active');
			$shop->date_create= strtotime('now');
			$shop->date_validate= strtotime($this->input->post('aa').'-'.$this->input->post('mm').'-'.$this->input->post('jj').'-'.$this->input->post('aa').' '.$this->input->post('hh').':'.$this->input->post('mn').' 00');
			$shop->shop_tag= $this->input->post('shop_tag');
			$shop->shop_keywords= $this->input->post('shop_keywords');
			$shop->siren= $this->input->post('siren');
			$shop->adresse= $this->input->post('adresse');
			$shop->region= $this->input->post('region');
			$shop->departement= $this->input->post('departement');
			$shop->code_postal= $this->input->post('code_postal');
			$shop->email= $this->input->post('email');
			$shop->password= $this->input->post('password');
			$shop->frist_name= ucwords(strtolower($this->input->post('frist_name')));
			$shop->last_name= strtoupper($this->input->post('last_name'));
			$shop->site= $this->input->post('site');
			$shop->open= $this->input->post('open');
			$shop->category_id= $this->input->post('category_id');
			$shop->telephone= $this->input->post('telephone');			
			$shop->youtube= $this->input->post('youtube');
			
			$delLogo=$this->input->post('delPhoto');
			foreach ($delLogo as $nameLogoPhoto){

				if(file_exists('./images/logos/thumbnails/'.$nameLogoPhoto))
					unlink('./images/logos/thumbnails/'.$nameLogoPhoto);
				if(file_exists('./images/logos/'.$nameLogoPhoto))
					unlink('./images/logos/'.$nameLogoPhoto);
				$shop->logo=  '';
			}
			$photos=  $this->input->post('photo');
			if(!empty($photos))
				foreach ($photos as $photo){
					$shop->logo=$photo;
					break;
				}
			$this->model()->edit($shop);
			global $CFG;
			$url=$CFG->config['base_url'].'shop/admin';
			redirect($url);
		}
		public function edit($id){
			$adminMenu= $this->load->module('adminMenu/index');
			$category= $this->load->module('category/admin');
		
			$shop= $this->model()->get_info_shop($id);
			$localisation= $this->load->module('localisation/index');
			$location=$localisation->getlocationCodePostal($shop->code_postal);
			$regionList=$location->OptionRegion();
			$departementList=$location->OptionDepartement();
			$codePostal=$location->Postalcode;
			$villeList=$location->OptionVille($codePostal);
		

			$setting =$this->load->Module('setting/admin');
			$this->template=$this->config->config['admin_template'];
			
			$data=array(
					'update_footer'=>'StarView 2013',
					'admin_title'=>'Boutique Modifier',
					'shop'=>$shop,
					'admin_menu'=>$adminMenu->generateAdminMenu(),
					'regionList'=>$regionList,
					'departementList'=>$departementList,
					'villeList'=>$villeList,
					'codePostal'=>$codePostal,
					'CategoryList'=>$category->dropdown_option_cats()
			);
		
			$this->view(
				array('admin-header',
						'shop-edit',
						'admin-footer'),
					$data); // Display the page
		}
		public function delete($id){

			$shop=$this->model()->get_info_shop($id);
			if(isset($shop->logo) and $shop->logo){
				$logoLink= dirname(BASEPATH)."/images/logos/";
				if(file_exists($logoLink.$shop->logo))
					unlink($logoLink.$shop->logo);
				if(file_exists($logoLink.'thumbnails/'.$shop->logo))
					unlink($logoLink.'thumbnails/'.$shop->logo);
			}
			if($id) $this->model()->delete($id);
		}
		public function deactive($id){
			$this->model()->deactive($id);
			$this->load->helper('url');
			redirect('shop/admin');
		}
		public function reactive($id){
			$this->model()->reactive($id);
			$this->load->helper('url');
			redirect('shop/admin');
		}
}